package eteam.aps.systemservice.dal;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import eteam.aps.systeminterface.model.entity.ModuleEntity;
import eteam.aps.systeminterface.model.enums.PlatformEnum;
import eteam.aps.systeminterface.model.enums.StateEnum;
import eteam.aps.systemservice.dal.mapper.IModuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 模块数据操作类
 */
@Service
public class ModuleDal {
    @Autowired
    private IModuleMapper moduleMapper;

    /**
     * 根据用户ID获取模块列表
     *
     * @return: 模块列表
     */
    public List<ModuleEntity> getWebModulesByUserId(String userid) {
        Map<String, Object> map = new IdentityHashMap<>();
        map.put("State", StateEnum.Enable.getValue());
        map.put("Platform", PlatformEnum.Web.getValue());
        StringBuffer sb = new StringBuffer();
        sb.append("select distinct t1.FModuleId from sp_moduleoper t1");
        sb.append(" inner join sp_funacl t2 on t1.FOperation=t2.FOperation");
        sb.append(" inner join sp_groupuser t3 on t2.FUserId=t3.FGroupId and t3.FUserId='" + userid + "'");
        return moduleMapper.selectList(new QueryWrapper<ModuleEntity>()
                .allEq(map)
                .inSql("FModuleId", sb.toString())
        ).stream().collect(Collectors.toList());
    }

    /**
     * 获取所有模块列表
     *
     * @return: 模块列表
     */
    public List<ModuleEntity> getWebModules() {
        Map<String, Object> map = new IdentityHashMap<>();
        map.put("State", StateEnum.Enable.getValue());
        map.put("Platform", PlatformEnum.Web.getValue());
        return moduleMapper.selectList(new QueryWrapper<ModuleEntity>().allEq(map)).stream().collect(Collectors.toList());
    }
}
